<template>
	<view>
		<uni-popup :mask-click="false" type="center" ref="updata">
			<view class="" style="position: relative;">
				<view class="posiab colorfff size50 mt100 ml50 bold zindex1000">发现新版本</view>
				<image mode="widthFix" style="width: 500rpx;" class="flex" src="./bg_top.png"></image>
				<view class="flex-ju-c bgfff height30 mt-30 size26 pt170" style="border-bottom-left-radius: 10px;border-bottom-right-radius: 10px;">
					<view @click="doUpData" class="srgb flex-ju-c bold bg000 colorfff radius80" style="position: absolute;width:90%;height: 100rpx;bottom: 40rpx;z-index: 5;">
						{{progress===500?'下载失败，请重试':progress===0?'立即更新':('正在更新...'+progress+'%')}}
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
	
</template>

<script>
	export default {
		data () {
			return {
				forceable:true,
				version:"",
				content:[],
				androidUrl:"",
				iosUrl:"",
				visible: false,
				progress: 0,
				flat:false,
				appstore:"",
				os_type:"",
				package_type:""
			}
		},
		
		mounted () {
			this.getversion()
		},
		methods: {
			getversion(){ 
				
				// this.http('setting/qrcode').then(res=>{
				// 	if(res.data.setting.hidden == 1){
				// 		this.$refs.updata.open()
				// 		uni.hideTabBar()
				// 	}
				// })
				
				 let version = uni.getStorageSync('version')?uni.getStorageSync('version'):plus.runtime.version
				
				this.http('version/app/check-update',{
					 version,
					 type:plus.os.name.toLowerCase()
				},'post','1').then(res=>{
					if(plus.os.name.toLowerCase() == 'ios'){
							if(res.data.is_has_update == 1){
								var prevVersion = this.fixZeroEnd(uni.getStorageSync('version'), 6).replace(/\./g,'')
								var nextVersion = this.fixZeroEnd(res.data.new_version.version, 6).replace(/\./g,'')
								if (Number(prevVersion) < Number(nextVersion)) {
									this.package_type = res.data.new_version.package_type
									this.androidUrl = res.data.new_version.package_url
									this.os_type = res.data.new_version.os_type
									this.appstore = res.data.new_version.appstore
									this.content = res.data.new_version.desc
									this.version = res.data.new_version.version
									this.$refs.updata.open()
									uni.hideTabBar()
								}
							}else{
								this.$refs.updata.close()
							}
						 
					}else{
						if(res.data.is_has_update == 1){
							var prevVersion = this.fixZeroEnd(uni.getStorageSync('version'), 6).replace(/\./g,'')
							var nextVersion = this.fixZeroEnd(res.data.new_version.version, 6).replace(/\./g,'')
							if (Number(prevVersion) < Number(nextVersion)) {
								this.package_type = res.data.new_version.package_type
								this.androidUrl = res.data.new_version.package_url
								this.os_type = res.data.new_version.os_type
								this.appstore = res.data.new_version.appstore
								this.content = res.data.new_version.desc
								this.version = res.data.new_version.version
								this.$refs.updata.open()
								uni.hideTabBar()
							}
						}else{
							this.$refs.updata.close()
						}
					}
					
				}).catch(res=>{
					console.log(22222)
					console.log(res)
				})
			},
			fixZeroEnd (str,n) {
				return (str + Array(n).join(0)).slice(0, n)
			},
			doUpData() {
				var prevVersion = this.fixZeroEnd(plus.runtime.version, 6).replace(/\./g,'')
				// if (Number(prevVersion) <= 999) {
				// 	if(this.os_type == 'ios'){
				// 		plus.runtime.openURL(this.appstore)
				// 	}else{
				// 		plus.runtime.openURL(this.androidUrl, err=>{
				// 					this.openURL(this.androidUrl)
				// 		},'com.android.browser')
				// 	}
				// 	return
				// }else{
					
					if(this.package_type == 'wgt'){
						 
					}else{
						if(this.os_type == 'ios'){
							plus.runtime.openURL(this.appstore)
						}else{
							plus.runtime.openURL(this.androidUrl, err=>{
									this.openURL(this.androidUrl)
						},'com.android.browser')
						}
						return
					}
					
				//}
				
				if (this.flat) {
					return
				}
				
				this.flat = true
				// 整包 
				var loadflag = false
				var _this = this
				var osname = plus.os.name
				
					uni.showLoading({
						mask:true
					})
					// 安卓打开网页下载
					
					//plus.runtime.openURL(this.downloadUrl_android);
					var downloadTask = uni.downloadFile({//执行下载
						url: _this.androidUrl, //下载地址
						success: downloadResult => {//下载成功
							if (downloadResult.statusCode === 200) {  
							plus.runtime.install(downloadResult.tempFilePath, {  
								force: false  
							}, function(e) {  
								uni.setStorageSync('version',_this.version)
								_this.visible = false
								_this.$refs.updata.close()
								setTimeout(()=>{
									plus.runtime.restart();
									uni.hideLoading()
								},100)
							}, function(e) {  
								uni.showModal({
									content:JSON.stringify(e)
								})
							});  
						}  
						},
						complete (e) {
							
						}
					})
					setTimeout(() => {
						if (!loadflag) {
							this.progress = 500
							uni.hideLoading()
							this.flat = false
						}
					}, 10000)
					downloadTask.onProgressUpdate(res => {
						loadflag = true
						if (res && res.progress) {
							this.progress = res.progress
						}
					})
				
			},
			close () {
				uni.showTabBar()
				this.visible = false
				this.$refs.updata.close()
			}
		}
	}
</script>

<style scoped>
.cu-modal {
	position: fixed;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	z-index: 1110;
	opacity: 0;
	outline: 0;
	text-align: center;
	-ms-transform: scale(1.185);
	transform: scale(1.185);
	backface-visibility: hidden;
	perspective: 2000upx;
	background: rgba(0, 0, 0, 0.6);
	transition: all 0.3s ease-in-out 0s;
	pointer-events: none;
}

.cu-modal::before {
	content: "\200B";
	display: inline-block;
	height: 100%;
	vertical-align: middle;
}

.cu-modal.show {
	opacity: 1;
	transition-duration: 0.3s;
	-ms-transform: scale(1);
	transform: scale(1);
	overflow-x: hidden;
	overflow-y: auto;
	pointer-events: auto;
}

.cu-dialog {
	position: relative;
	display: inline-block;
	vertical-align: middle;
	margin-left: auto;
	margin-right: auto;
	width: 680upx;
	max-width: 100%;
	background-color: #f8f8f8;
	border-radius: 10upx;
	overflow: hidden;
}
.cu-modal .cu-dialog>.cu-bar:first-child .action{
  min-width: 100rpx;
  margin-right: 0;
  min-height: 100rpx;
}
/* .upgrade { position: relative; min-height: 480rpx; overflow: visible; width: 550rpx; padding-top: 320rpx; background: url(./upgrade1.png) center 0 / 550rpx 504rpx no-repeat;} */
.upgrade image { position: absolute; top: -130rpx; right: 0; width: 550rpx; height: 504rpx;}
.version { font-size: 0; z-index:99999;}
.version .padding { font-size: 28rpx;}
.version .des { text-align: left; line-height: 36rpx; padding: 20rpx; font-size: 28rpx; line-height: 48rpx; color: #0B0A12;}
.updatebtn { background: linear-gradient(90deg, #1A86FF, #00B2FF); color: #fff; line-height: 80rpx; height: 80rpx; text-align: center; border-radius: 40rpx; font-size: 28rpx; margin: 0 auto 20rpx;}
.updatebtn_fail { background: #EE1600;}
.dialog_close { margin-bottom: 20rpx; color: #000; font-size: 48rpx;}
.bg-white { background-color: #fff;}
.padding-lr { padding: 0 30rpx;}
.padding-top { padding-top: 30rpx;}
</style>